package org.i1510.common.utils.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.betwixt.io.BeanReader;
import org.i1510.common.bean.AuthBeans;
import org.i1510.common.bean.SpringQuartzBean;
import org.i1510.common.bean.SpringQuartzPropertyBean;
import org.i1510.common.interceptor.AuthAccessInterceptor;
import org.i1510.common.po.SysPop;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.StringUtils;
import org.i1510.common.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.xml.sax.SAXException;

/**
 * @author Administrator
 * 权限xml文件导入工具
 */
public class AuthBeanUtils {
	private static Logger logger = LoggerFactory.getLogger(AuthBeanUtils.class);
	public static String CLASSPATH = "classpath:/config/*/authAccess.xml";

	/**
	 * @Author Rambo
	 * @Desc：读取loaction路径对应文件的Auth权限，如果为空取所有的authAccess.xml文件
	 * @param loaction
	 * @return TODO
	 * @Version AuthBeanUtils.java,v 1.1 2013-11-28 下午3:38:07
	 */
	public static List<SysPop> readAuth(String loaction) {
		List<SysPop> list = new ArrayList<SysPop>();

		//FileManager fm = new FileManager();
		
		
		try {

			ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
			Resource[] source = resourceLoader.getResources(StringUtils
					.isStringEmpty(loaction) ? CLASSPATH : loaction);
			for (int i = 0; i < source.length; i++) {
				// 创建一个BeanReader实例，相当于转换器
				BeanReader beanReader = new BeanReader();
				AuthBeans beans = new AuthBeans();
				beanReader.push(beans);
				// 增加规则
				addRules1(beanReader);
				Resource resource = source[i];
				File file = resource.getFile();
				if(logger.isDebugEnabled()){
					logger.info(file.getPath());
				}				
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					beanReader.parse(file);
					for (int j = 0; j < beans.getBeans().size(); j++) {
						SysPop p = (SysPop) beans.getBeans().get(j);
						System.out.println(p.getDocId()+"-"+p.getDocSubject());
						list.add(p);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public static List<SysPop> readAuth4Request(String loaction,HttpServletRequest request) {
		List<SysPop> list = new ArrayList<SysPop>();

		//FileManager fm = new FileManager();
		
		
		try {

			ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
			Resource[] source = resourceLoader.getResources(StringUtils
					.isStringEmpty(loaction) ? CLASSPATH : loaction);
			for (int i = 0; i < source.length; i++) {
				// 创建一个BeanReader实例，相当于转换器
				BeanReader beanReader = new BeanReader();
				AuthBeans beans = new AuthBeans();
				beanReader.push(beans);
				// 增加规则
				addRules1(beanReader);
				Resource resource = source[i];
				File file = resource.getFile();
				if(logger.isDebugEnabled()){
					logger.info(file.getPath());
				}				
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					beanReader.parse(file);
					for (int j = 0; j < beans.getBeans().size(); j++) {
						SysPop p = (SysPop) beans.getBeans().get(j);
						p.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
						p.setDocCreateTime(DateUtils.getNow());
						System.out.println(p.getDocId()+"-"+p.getDocSubject());
						list.add(p);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * @Author Rambo
	 * @Desc：xml格式规则1
	 * @param d
	 *            TODO
	 * @Version AuthBeanUtils.java,v 1.1 2013-11-28 下午3:41:34
	 */
	private static void addRules1(BeanReader d) {
		// 当遇到<person>时，创建类Person的一个实例，并将其压入栈顶
		d.addObjectCreate("beans/bean", SysPop.class);
		d.addSetProperties("beans/bean");
		d.addSetNext("beans/bean", "addBean");

		d.addCallMethod("beans/bean/docId", "setDocId", 0);
		d.addCallMethod("beans/bean/docSubject", "setDocSubject", 0);
		d.addCallMethod("beans/bean/descs", "setDescs", 0);
		d.addCallMethod("beans/bean/url", "setUrl", 0);
		d.addCallMethod("beans/bean/javaClass", "setJavaClass", 0);
		d.addCallMethod("beans/bean/javaMethod", "setJavaMethod", 0);
		d.addCallMethod("beans/bean/parentId", "setParentId", 0);
		d.addCallMethod("beans/bean/hierarchyId", "setHierarchyId", 0);
		d.addCallMethod("beans/bean/isMenu", "setIsMenu", 0);
		d.addCallMethod("beans/bean/isValid", "setIsValid", 0);
		d.addCallMethod("beans/bean/docStatus", "setDocStatus", 0);
		
	}
	public static final void main1(String args[]) throws Exception {
		String path = "/config/common/authAccess.xml";
		// 创建一个BeanReader实例，相当于转换器
		BeanReader beanReader = new BeanReader();
		AuthBeans beans = new AuthBeans();
		beanReader.push(beans);
		// 增加规则
		addRules1(beanReader);
		File file = new File(AuthBeanUtils.class.getResource("/").getPath()
				+ path);
		if (file.isFile() && file.exists()) { // 判断文件是否存在
			beanReader.parse(file);

			for (int i = 0; i < beans.getBeans().size(); i++) {
				SysPop p = (SysPop) beans.getBeans().get(i);
				System.out.println(p.getDocId()+"-"+p.getDocSubject());
			}
		}

	}
	public static final void main(String args[]) throws Exception {
		AuthBeanUtils.readAuth("/config/common/authAccess.xml");

	}

}
